package com.foxcai.novel.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.foxcai.novel.base.ConfConstants;
import com.foxcai.novel.base.DBConnection;
import com.foxcai.novel.html.JspContextServices;
import com.foxcai.novel.http.EasouHttpCommAction;

public class GetNovelDetailToHtm {

	public static String InsertToDb(String xml) {

		JspContextServices utb = new JspContextServices();

		Document doc = null;
		StringBuffer novHtm = new StringBuffer(
				"<div class=\"contentlist\">\r\n");
		try {
			doc = DocumentHelper.parseText(xml);
			Element root = doc.getRootElement();
			String NovId = root.elementText("novelid");
			System.out.println("novelid:" + NovId);
			if (!NovId.equals("")) {

				novHtm.append("<div class=\"title\"><h1>"
						+ root.elementTextTrim("chaptername")
						+ "</h1></div>\r\n");
				novHtm.append("<div class=\"info\">作者："
						+ root.elementTextTrim("author")
						+ "&nbsp;&nbsp;&nbsp;&nbsp;\r\n");
				novHtm.append("更新时间："
						+ utb.getServerTime("yyyy-MM-dd HH:mm:ss")
						+ "</div>\r\n");
				novHtm.append("<div class=\"content\">"
						+ root.elementText("content") + "</div>\r\n");
				novHtm.append("</div>\r\n");
				novHtm.append("<div class=\"source\">\r\n");
				novHtm.append("来源：" + root.elementText("url") + "\r\n");

				String tempPath = "";
				String outPath = "";

				tempPath = ConfConstants.CONTEXT_PATH + "\\template\\view.htm";
				outPath = ConfConstants.CONTEXT_PATH + "\\info\\"
						+ root.elementTextTrim("novelid") + ".html";

				String nTitle = "" + root.elementTextTrim("chaptername") + "-"
						+ root.elementTextTrim("novelname")
						+ "-SoDu小说搜索-最好的小说搜索网站";
				String templateContent = "";
				FileInputStream fileinputstream = new FileInputStream(tempPath);// 读取模块文件
				int lenght = fileinputstream.available();
				byte bytes[] = new byte[lenght];
				fileinputstream.read(bytes);
				fileinputstream.close();
				templateContent = new String(bytes);
				templateContent = templateContent.replaceAll("##Title##",
						nTitle);
				templateContent = templateContent.replaceAll("##ContentBody##",
						novHtm.toString());

				FileOutputStream fileoutputstream = new FileOutputStream(
						outPath);// 建立文件输出流
				byte tag_bytes[] = templateContent.getBytes();
				fileoutputstream.write(tag_bytes);
				fileoutputstream.close();

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			doc.clearContent();
		}
		novHtm.append("</div>\r\n");

		return novHtm.toString();

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		DBConnection conn = null;

		ResultSet rs = null;
		String reqUrl = "http://b1.easou.com/getChapterInfoImpl.m";
		String reqParams = null;
		String resultXml = null;
		EasouHttpCommAction ehca = new EasouHttpCommAction();
		long beg = System.currentTimeMillis();

		try {
//			conn = DBConnection.newInstance();
//			String sql = "select NovId from NovelList order by NovAcc desc";
//
//			rs = conn.executeQuery(sql);
//			while (rs.next()) {
//				reqParams = "id=" + rs.getInt("NovId") + "";
//				System.out.println("reqParams:" + reqParams);
//				resultXml = ehca.send(reqParams, reqUrl);
//				Thread.sleep(1000);
//				InsertToDb(resultXml);
//			}
			resultXml = ehca.send("id=2114779", reqUrl);
			InsertToDb(resultXml);

//		} catch (SQLException e) {
//			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		long curTime = System.currentTimeMillis();
		long intvl = curTime - beg;
		System.out.println("10条数据耗时：" + intvl + "毫秒！！");

	}
}
